package org.ekstrabilet.stadium;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import org.ekstrabilet.logic.exceptions.StadiumException;
import org.ekstrabilet.stadium.constants.Panes;
import org.ekstrabilet.stadium.constants.StadiumConstants;
import org.ekstrabilet.stadium.constants.StadiumState;
import org.ekstrabilet.stadium.logic.StadiumLogic;


/**
 * 
 * @author Maciej Koch
 * 
 * GUI class for stadium panel. Contains <b>StadiumTechPane</b> to set parameters of the new stadium.
 * Also contains <b>StadiumViewPane</b> to display a previews. From here user can save new stadium.
 */
public class StadiumPane extends JPanel implements ActionListener {
	
	JPanel all;
	JPanel buttons;
	int state;
	JPanel cards;
	StadiumTechPane techPane;
	StadiumViewPane viewPane;
	
	JButton next;
	JButton prev;
	JButton save;
	
	public StadiumPane() {
		init();
		
	}
	
	/**
	 * Sets current state 
	 * @param state
	 * -to set
	 */
	public void setState(int state) {
		this.state = state;
		switchPane();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(prev.equals(e.getSource()))
			state--;
		else
			state++;
		
		switch(state) {
		case StadiumState.TECH:
			break;
		case StadiumState.VIEW:
			viewPane.setStadium(techPane.stadiumPrepering());
			break;
		}
		switchPane();
	}
	
	private void init() {
		
		all = new JPanel();
		all.setLayout(new GridLayout(2, 1));
				
		cards = new JPanel();
		cards.setPreferredSize(new Dimension(StadiumConstants.viewWidth, StadiumConstants.cardViewHeight));

		cards.setLayout(new CardLayout());
		techPane = new StadiumTechPane();
		viewPane = new StadiumViewPane(false, null);
		cards.add(techPane, Panes.TECH);
		cards.add(viewPane, Panes.VIEW);
		
		all.add(cards);		
		buttons = new JPanel();

		next = new JButton("next");
		prev = new JButton("back");
		next.addActionListener(this);
		prev.addActionListener(this);
		buttons.add(prev);
		buttons.add(next);
		
		save = new JButton("save");
		save.addActionListener(new SaveListener());
		buttons.add(save);
		
		all.add(buttons);
		
		
		
		add(all);
		
		setState(StadiumState.TECH);
		switchPane();
	}
	private void switchPane() {
		
		CardLayout cl = (CardLayout) cards.getLayout();
		switch(state) {
		case StadiumState.TECH:
			cl.show(cards, Panes.TECH);
			break;
		case StadiumState.VIEW:
			cl.show(cards, Panes.VIEW);
			break;
		}
		setButtonsVisible();
		repaint();
	}

	private void setButtonsVisible(){
		if(state == StadiumState.TECH){
			prev.setVisible(false);
			save.setVisible(false);
			next.setVisible(true);
		}
		else {
			prev.setVisible(true);
			save.setVisible(true);
			next.setVisible(false);
		}
			
	}
	private class SaveListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {

			StadiumLogic logic = StadiumLogic.getInstance();
			try {
				logic.addStadium(techPane.stadiumPrepering());
			} catch (StadiumException e1) {
				JOptionPane.showMessageDialog(cards, e1.getMessage());
				return;
			}
			JOptionPane.showMessageDialog(cards, "Succesful!");
		}
		
	}
}
